package com.lgc.provider01.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @param
 * @author lvguangchun
 * @create 2022-08-29 13:49
 */
@Configuration//配置类
@EnableSwagger2//开启Swagger2的自动配置
public class SwaggerConfig {

    //配置不同分组
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    //配置Swagger2的Docket的Bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示swagger的环境
        Profiles of = Profiles.of("dev","default");
        //判断当前是否处于改环境
        //通过enable()接受参数判断是否要显示
        boolean b = environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //指定文档所属分组
                .groupName("lgc")
                //配置是否启用Swagger，如果是false，在浏览器将无法访问；默认值为true
                .enable(b)
                .select()
                //RequestHandlerSelectors: 配置要扫描接口的方式
                //basePackage:指定要扫描的接口
                .apis(RequestHandlerSelectors.basePackage("com.lgc.provider01.controller"))
                //配置如何通过path过滤，即这里只扫描请求以/lgc开头的接口
//                .paths(PathSelectors.ant("/lgc/**"))
                .build();
    }

    //配置文档基本信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("lgc", "https://gitee.com/sight6/spring-cloud-study-s.git", "lvguangchun@foxmail.com");
        return new ApiInfo(
                "Swagger学习", // 标题
                "学习演示如何配置Swagger", // 描述
                "v1.0", // 版本
                "https://gitee.com/sight6/spring-cloud-study-s.git", // 组织链接
                contact, // 联系人信息
                "Apach 2.0 许可", // 许可
                "http://www.apache.org/licenses/LICENSE-2.0", // 许可连接
                new ArrayList<>()// 扩展
        );
    }
}
